package com.dataseek.iot.device.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.device.entity.DayLevel;
import com.dataseek.iot.device.entity.DayUse;
import com.dataseek.iot.device.entity.LiquidJar;
import com.dataseek.iot.device.mapper.DayUseMapper;
import com.dataseek.iot.device.mapper.DeviceAttValueHisMapper;
import com.dataseek.iot.device.mapper.LiquidJarMapper;
import com.dataseek.iot.device.service.DayLevelService;
import com.dataseek.iot.device.service.DayUseService;
import com.dataseek.iot.device.service.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

@Service
@EnableScheduling
@Slf4j
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private DeviceAttValueHisMapper deviceAttValueHisMapper;

    @Autowired
    private DayLevelService dayLevelService;

    @Autowired
    private DayUseMapper dayUseMapper;

    @Autowired
    LiquidJarMapper liquidJarMapper;

    //@Scheduled(fixedRate = 1000*20) 每隔20秒
    //1小时计算一次,取上1小时内最后一次的液位
    @Scheduled(cron="0 0 */1 * * ?")
    public void do1HourAction() {
        List<DayLevel> levelList = deviceAttValueHisMapper.query1HourCalValue();
        if (!CollectionUtils.isEmpty(levelList)) {
            dayLevelService.insertBatch(levelList);
        }
    }

    //每5分钟计算一次使用量
    @Scheduled(cron="0 */5 * * * ?")
    public void do5MinAction() {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int min = cal.get(Calendar.MINUTE);
        String calDate = this.getCalDate(cal);

        List<DayLevel> levelList = deviceAttValueHisMapper.query5CalValue();
        if (!CollectionUtils.isEmpty(levelList)) {
            QueryWrapper<LiquidJar> qwJar = new QueryWrapper<>();
            for (DayLevel levelDto : levelList) {
                //每天00:00:00的时候，新增一条记录
                if (hour == 0 && min == 0) {
                    DayUse useDto = new DayUse();
                    useDto.setJarCode(levelDto.getJarCode());
                    useDto.setDeviceCode(levelDto.getDeviceCode());
                    useDto.setLastLevel(levelDto.getLevel());
                    useDto.setUsedAmount(BigDecimal.ZERO);
                    useDto.setUsedAmountVol(BigDecimal.ZERO);
                    useDto.setCalDate(calDate);
                    dayUseMapper.insert(useDto);
                }
                else {
                    DayUse qryDto = new DayUse();
                    qryDto.setJarCode(levelDto.getJarCode());
                    qryDto.setDeviceCode(levelDto.getDeviceCode());
                    qryDto.setCalDate(calDate);
                    QueryWrapper<DayUse> queryWrapper = new QueryWrapper<>(qryDto);
                    DayUse useDto = dayUseMapper.selectOne(queryWrapper);
                    if (useDto != null) {
                        BigDecimal curLevel = levelDto.getLevel();
                        BigDecimal lastLevel = useDto.getLastLevel();
                        BigDecimal usedAmount = useDto.getUsedAmount();
                        //如果当前液位大于上次业务，表明加液了，不做处理
                        if (curLevel.compareTo(lastLevel) > 0) {

                        }
                        else {
                            usedAmount = usedAmount.add(lastLevel.subtract(curLevel)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        }
                        useDto.setLastLevel(curLevel);
                        useDto.setUsedAmount(usedAmount);

                        //获取储液罐信息
                        qwJar.eq("jar_code", levelDto.getJarCode());
                        LiquidJar jarDto = liquidJarMapper.selectOne(qwJar);
                        if (jarDto != null && jarDto.getJarProportion() != null) {
                            useDto.setUsedAmountVol(useDto.getUsedAmount().multiply(new BigDecimal(jarDto.getJarProportion()))
                                    .setScale(2,BigDecimal.ROUND_HALF_UP));
                        }
                        dayUseMapper.updateById(useDto);
                    }
                    else {
                        useDto = new DayUse();
                        useDto.setJarCode(levelDto.getJarCode());
                        useDto.setDeviceCode(levelDto.getDeviceCode());
                        useDto.setLastLevel(levelDto.getLevel());
                        useDto.setUsedAmount(BigDecimal.ZERO);
                        useDto.setUsedAmountVol(BigDecimal.ZERO);
                        useDto.setCalDate(calDate);
                        dayUseMapper.insert(useDto);
                    }
                }
            }
        }
    }

    private String getCalDate(Calendar cal) {
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String yearStr = String.valueOf(year);
        String monthStr = month > 9 ? String.valueOf(month) : "0" + String.valueOf(month);
        String dayStr =  day > 9 ? String.valueOf(day) : "0" + String.valueOf(day);
        return yearStr + "-" +  monthStr + "-" + dayStr;
    }

}
